package cloudContact.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mybeans.factory.RollbackException;

import cloudContact.Common;
import cloudContact.MyDAOException;
import cloudContact.DAO.GoogleContact;
import cloudContact.DAO.GoogleContactDAO;
import cloudContact.DAO.User;
import cloudContact.DAO.UserDAO;

import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.data.contacts.ContactGroupEntry;
import com.google.gdata.util.ServiceException;
import com.google.gson.Gson;

import cloudContact.SecurityManager;
import cloudContact.GoogleConstant;

/**
 * Servlet implementation class GoogleContactGroup
 */
@WebServlet("/GoogleContactGroup")
public class GoogleContactGroup extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GoogleContactGroup() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Start GoogleContactGroup...");

		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		try {
			if (!SecurityManager.CheckAuthorization(request)) {
				out.println("Error: User is not authorized.");
				return;
			}

			HttpSession session = request.getSession();
			
			User user = null;
			try {			
				 user = UserDAO.getInstance().lookup(session.getAttribute("userId").toString());			
			} catch (RollbackException e) {
				e.printStackTrace();
			} catch (MyDAOException e) {
				e.printStackTrace();
			}

			if (user == null) {
				out.println("Error: User not found.");
				return;
			}
			
			System.out.println(user.getGMailAccessToken());
			System.out.println(user.getGMailAuthSecrete());
			
			GoogleOAuthParameters oauthParameters = GoogleConstant.getGoogleOAuthParameters(user.getGMailAccessToken(), user.getGMailAuthSecrete(), user.getUserId());
						
			GoogleContactDAO gContactDAO = GoogleContactDAO.getInstanceForSession(session.getId(), oauthParameters);

			Set<ContactGroupEntry> groupList = gContactDAO
					.getAllContactsWithGroup().keySet();

			Hashtable<String, String> shortGroup = new Hashtable<String, String>();
			for (ContactGroupEntry group : groupList) {
				shortGroup.put(group.getId(), group.getTitle().getPlainText());
			}

			Gson gSon = new Gson();
			out.print(gSon.toJson(shortGroup));

		} catch (OAuthException e) {
			out.print(Common.CriticalErrorRaised(e));
		} catch (ServiceException e) {
			out.print(Common.CriticalErrorRaised(e));
		} catch (RollbackException e) {
			out.print(Common.CriticalErrorRaised(e));
		} catch (MyDAOException e) {
			out.print(Common.CriticalErrorRaised(e));
		} finally {
			out.flush();
			out.close();
			System.out.println("Finish GoogleContactGroup.");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
